package chat02;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2024-09-12
 * Time: 22:52
 */
public class ForwardMsg extends Thread {

    private Socket socket;

    public ForwardMsg(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        // 发送消息给服务器
        while(true){

            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){

                String msg = sc.next();
                // 获取流    流需要通过socket对象获取
                try {
                    OutputStream outputStream = socket.getOutputStream();

                    outputStream.write(msg.getBytes());

                    outputStream.flush();
                    System.out.println("客户端发送消息成功！");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}
